package com.czr.dao.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czr.dao.entity.CzrColumn;
import com.czr.dao.entity.CzrNews;
import com.czr.dao.entity.CzrPost;
import com.czr.dao.entity.CzrReply;
import com.czr.dao.mapper.*;
import com.czr.dao.service.CzrColumnService;
import com.czr.dao.service.CzrNewsService;
import com.czr.dao.service.CzrPostService;
import com.czr.dao.service.CzrReplyService;
import com.czr.dto.ColumnParam;
import com.czr.dto.Search;
import com.czr.util.PageUtil;
import com.czr.util.TencentCosUtil;
import com.czr.vo.CzrNewsVo;
import com.czr.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 栏目表 服务实现类
 * </p>
 */
@Service
public class CzrColumnServiceImpl extends ServiceImpl<CzrColumnMapper, CzrColumn> implements CzrColumnService {

    @Autowired
    public CzrColumnMapper czrColumnMapper;
    @Autowired
    public CzrReplyMapper czrReplyMapper;

    @Autowired
    public CzrPostMapper czrPostMapper;

    @Autowired
    public CzrUserMapper czrUserMapper;

    @Override
    public Result<Object> dictionary() throws Exception {
        List<CzrColumn> czrColumn = this.list();
        return Result.data(200, czrColumn, null);
    }

    @Override
    public Result<Object> updateColumn(String param) {
        //解析
        if (StringUtils.isEmpty(param)) {
            return null;
        }
        try {
            JSONArray array = JSONArray.parseArray(param);
            for (Object obj : array) {
                JSONObject object = JSONObject.parseObject(obj.toString());
                CzrColumn czrColumn = new CzrColumn();
                czrColumn.setId(object.getLong("id"));
                czrColumn.setName(object.getString("name"));
                czrColumn.setSynopsis(object.getString("synopsis"));
                czrColumn.updateById();
            }
        } catch (Exception e) {
            return Result.success(200, "修改失败！");
        }


        return Result.success(200, "修改成功！");
    }

    @Override
    public Result<Object> statistics(Integer columuId) throws Exception {
        Integer tiezi = czrReplyMapper.getPostColumnCount(columuId);
        Integer huifu = czrReplyMapper.getReplyColumnCount(columuId);
        Integer dianzan = czrReplyMapper.getLikeColumnCount(columuId);
        JSONObject resultObj = new JSONObject();
        resultObj.put("tiezi", tiezi);
        resultObj.put("huifu", huifu);
        resultObj.put("dianzan", dianzan);
        return Result.data(200, resultObj, "");
    }

    @Override
    public Result<Object> postRanking(Integer columuId) throws Exception {
        TencentCosUtil tencentCosUtil=new TencentCosUtil();
        List<Map<String, Object>> mapList = czrPostMapper.getPostColumnCount(columuId);
        JSONArray jsonArray=new JSONArray();
        for(Map<String,Object> map:mapList){
            if (!StringUtils.isEmpty(map)) {
                JSONObject resultObj = new JSONObject();
                Integer num = czrReplyMapper.getUserReplyColumnCount(columuId, Integer.parseInt(map.get("userId").toString()));
                Map<String, Object> map1 = czrUserMapper.getUserInfo(Long.parseLong(map.get("userId").toString()));
                if (!StringUtils.isEmpty(map1)) {
                    resultObj.put("nick_name", map1.get("nick_name").toString());
                    resultObj.put("user_logo", tencentCosUtil.imgFile_wx(map1.get("user_logo").toString()));
                }
                resultObj.put("postNum", map.get("num"));
                resultObj.put("replyNum", num);
                jsonArray.add(resultObj);
            }
        }

        return Result.data(200, jsonArray, "");
    }

    @Override
    public Result<Object> latestComments(Integer columuId) throws Exception {
        TencentCosUtil tencentCosUtil=new TencentCosUtil();
        List<CzrReply> mapList = czrReplyMapper.getColumnRepalyList(columuId);
        JSONArray jsonArray=new JSONArray();
        for(CzrReply czrReply:mapList){
            if (!StringUtils.isEmpty(czrReply)) {
                JSONObject resultObj = new JSONObject();
                Map<String, Object> map1 = czrUserMapper.getUserInfo(Long.parseLong(czrReply.getUserId().toString()));
                if (!StringUtils.isEmpty(map1)) {
                    resultObj.put("nick_name", map1.get("nick_name").toString());
                    resultObj.put("user_logo", tencentCosUtil.imgFile_wx(map1.get("user_logo").toString()));
                }
                resultObj.put("comment", czrReply.getComment());
                resultObj.put("pubTime", czrReply.getPubTime());
                jsonArray.add(resultObj);
            }
        }

        return Result.data(200, jsonArray, "");
    }

    @Override
    public Result<Object> list(ColumnParam search) throws Exception {
        Page<CzrColumn> page = new Page<>();
        page.setSize(search.getSize());
        page.setCurrent(search.getCurrent());
        LambdaQueryWrapper<CzrColumn> ew = new LambdaQueryWrapper<>();
        if(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(search.getKeyword())){
            ew.like(CzrColumn::getName,search.getKeyword());
        }
        if(!StringUtils.isEmpty(search.getParentId())){
            ew.eq(CzrColumn::getParentId,search.getParentId());
        }
        page = this.getBaseMapper().selectPage(page,ew);
        JSONArray array=new JSONArray();
        for (CzrColumn column : page.getRecords()) {
            CzrColumn CzrColumnVO = new CzrColumn();
            BeanUtils.copyProperties(column, CzrColumnVO);
            array.add(CzrColumnVO);
        }
        JSONObject object= PageUtil.getPage(array,page);
        return Result.data(object);
    }
}
